class Solution:
    def isPowerOfThree(self, n: int) -> bool:
        if n <= 0:
            return False

        if n == 1:
            return True

        while True:
            temp = n % 3
            if temp != 0:
                return False

            n //= 3
            if n == 1:
                return True